package com.amarsoft.are.log.impl.simple;

import java.io.PrintWriter;
import java.io.StringWriter;

public class SimpleFormatter extends Formatter
{
  public synchronized String format(LogRecord paramLogRecord)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    int i = inferCaller(paramLogRecord);
    String str = paramLogRecord.getLoggerName();
    if (str != null)
    {
      localStringBuffer.append(str);
      localStringBuffer.append(" ");
    }
    if (paramLogRecord.getSourceClassName() != null)
      localStringBuffer.append(paramLogRecord.getSourceClassName());
    if (paramLogRecord.getSourceMethodName() != null)
    {
      localStringBuffer.append('.');
      localStringBuffer.append(paramLogRecord.getSourceMethodName());
      if (i != -1)
        localStringBuffer.append("().").append(Integer.toString(i));
      else
        localStringBuffer.append("()");
    }
    localStringBuffer.append(' ');
    localStringBuffer.append(paramLogRecord.getMessage());
    if (paramLogRecord.getThrown() != null)
      try
      {
        StringWriter localStringWriter = new StringWriter();
        PrintWriter localPrintWriter = new PrintWriter(localStringWriter);
        paramLogRecord.getThrown().printStackTrace(localPrintWriter);
        localPrintWriter.close();
        localStringBuffer.append(localStringWriter.toString());
      }
      catch (Exception localException)
      {
      }
    return localStringBuffer.toString();
  }

  private int inferCaller(LogRecord paramLogRecord)
  {
    StackTraceElement[] arrayOfStackTraceElement = new Throwable().getStackTrace();
    int i = -1;
    Object localObject;
    for (int localStackTraceElement1 = 0; localStackTraceElement1 < arrayOfStackTraceElement.length; ++localStackTraceElement1)
    {
    	StackTraceElement localStackTraceElement2 = arrayOfStackTraceElement[localStackTraceElement1];
      localObject = localStackTraceElement2.getClassName();
      if (((String)localObject).equals("com.amarsoft.are.log.impl.SimpleLog"))
        break;
    }
    int localStackTraceElement1 = 0;
    while (localStackTraceElement1 < arrayOfStackTraceElement.length)
    {
    	StackTraceElement localStackTraceElement2 = arrayOfStackTraceElement[localStackTraceElement1];
      i = arrayOfStackTraceElement[localStackTraceElement1].getLineNumber();
      localObject = localStackTraceElement2.getClassName();
      if (!(((String)localObject).equals("com.amarsoft.are.log.impl.SimpleLog")))
        break;
      ++localStackTraceElement1;
    }
    if (localStackTraceElement1 == arrayOfStackTraceElement.length)
      return i;
    int localStackTraceElement2 = localStackTraceElement1;
    String str;
    while ((paramLogRecord.getMessage().matches(".*\\[JBO\\].*")) && (localStackTraceElement1 < arrayOfStackTraceElement.length))
    {
      localObject = arrayOfStackTraceElement[localStackTraceElement1];
      str = ((StackTraceElement)localObject).getClassName();
      if (!(str.startsWith("com.amarsoft.are.jbo.")))
      {
        paramLogRecord.setSourceClassName(str);
        paramLogRecord.setSourceMethodName(((StackTraceElement)localObject).getMethodName());
        i = arrayOfStackTraceElement[localStackTraceElement1].getLineNumber();
        return i;
      }
      ++localStackTraceElement1;
    }
    while ((paramLogRecord.getMessage().matches(".*\\[SQL\\d*\\].*")) && (localStackTraceElement1 < arrayOfStackTraceElement.length))
    {
      localObject = arrayOfStackTraceElement[localStackTraceElement1];
      str = ((StackTraceElement)localObject).getClassName();
      if ((!(str.startsWith("com.amarsoft.are.sql."))) && (!(str.startsWith("com.amarsoft.are.jbo."))) && (!(str.startsWith("com.amarsoft.awe.util."))))
      {
        paramLogRecord.setSourceClassName(str);
        paramLogRecord.setSourceMethodName(((StackTraceElement)localObject).getMethodName());
        i = arrayOfStackTraceElement[localStackTraceElement1].getLineNumber();
        return i;
      }
      ++localStackTraceElement1;
    }
    paramLogRecord.setSourceClassName(arrayOfStackTraceElement[localStackTraceElement2].getClassName());
    paramLogRecord.setSourceMethodName(arrayOfStackTraceElement[localStackTraceElement2].getMethodName());
    i = arrayOfStackTraceElement[localStackTraceElement2].getLineNumber();
    return i;
  }
}